<%@ page contentType="text/html;charset=utf-8" %>
<%@ page import="java.net.URLDecoder" %>
<%@ page import="net.sf.json.JSONObject" %>
<%@ page import="cn.com.gome.sn.task.utils.TaskTrigerUtils" %>
<%@ page import="cn.com.gome.sn.task.utils.BasisConstants" %>
<%@ page import="cn.com.gome.frame.util.MainSecurity" %>
<%@ page import="cn.com.gome.frame.util.StringUtils" %>

<%
    //当前页面包含四个逻辑：
    //1、登录
    //2、退出
    //3、查看列表
    //4、触发动作
%>
<%
    String s = request.getParameter("type");

    Cookie[] cookies = request.getCookies();
    boolean isLogin = false;
    if(cookies!=null)for(Cookie cookie : cookies){
        if(cookie.getName().equals("SN-taskadmin")){
            String val = cookie.getValue();
            try{
                val = MainSecurity.decode(val);
                isLogin = true;
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
    //未登录，且类型为login
    if(!isLogin&&(StringUtils.isEmpty(s)||!s.equals("login"))){
        %>
            <html>
                <head>
                    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                    <title>SN-taskadmin管理登录</title>
                </head>
                <body>
                    <form action="index.jsp?type=login" method="post">
                        帐号:<input name = "userName" type="text"><br>
                        密码:<input name = "password" type="password"><br>
                        <input type="submit" value="登录" >
                    </form>
                </body>
            </html>
        <%
        return ;
    }
    //登录动作，如果登录成功，重定向至/
    if(!isLogin&&s.equals("login")){
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        System.out.println("userName["+userName+"]password["+password+"]");
        if(userName.equals("admin")&&password.equals(MainSecurity.decode("axgh7wnjjRGqOMwPFYz4Qw=="))){
            System.out.println("OK");
            Cookie ck = new Cookie("SN-taskadmin",MainSecurity.encode(password));
            ck.setMaxAge(60 * 60);//1小时
            response.addCookie(ck);
            //登录成功
            %><html><script>window.location.href="./";</script></html><%
            return;
        }
        //没有登录
        %><html><script>window.location.href="./index.jsp";</script></html><%
        return;
    }
    //退出登录，重定向到index.jsp
    if(isLogin&&(StringUtils.isNotEmpty(s)&&s.equals("logout"))){
        Cookie ck = new Cookie("SN-taskadmin","out");
        ck.setMaxAge(0);
        response.addCookie(ck);
        %><html><script>window.location.href="./index.jsp";</script></html><%
        return;
    }

    //如果已登录，并且s为空或者为/
    if(isLogin&&StringUtils.isEmpty(s)){
        %>
        <html>
            <body>
            <jsp:useBean id="tBean" scope="page" class="cn.com.gome.sn.task.bean.GlobalTaskInfoConfig" ></jsp:useBean>
            <style>
                table{ border:solid 1px black}
                tr{ border:solid 1px black;padding: 0;margin: 0;}
                td{ border:solid 1px black;margin:0;cellspacing:0;}
            </style>
            <script src="js/jquery-1.7.1.min.js"></script>
            <script>
                $(function(){
                    $("table thead tr").css("background-color","#006030");
                    $("table thead tr").css("color","#FFFFFF");
                    $("table thead tr").css("font-weight","800");
                    $("table tbody tr:even").css("background-color","#DFFFDF");
                    $("table tbody tr:odd").css("background-color","#ECFFFF");
                    $("table tbody tr").mouseover(function() {
                        $("table tbody tr:even").css("background-color","#DFFFDF");
                        $("table tbody tr:odd").css("background-color","#ECFFFF");
                        $(this).css("background-color","#FFFF37");
                    });
                    $("table tbody tr").mouseout(function() {
                        $("table tbody tr:even").css("background-color","#DFFFDF");
                        $("table tbody tr:odd").css("background-color","#ECFFFF");
                    });
                })
            </script>
            <span><h2>全局定时任务监控页面</h2><a href="index.jsp?type=logout"><h6>退出登录</h6></a></span>

            <h5>任务执行情况</h5>
            <table>
                <tr>
                    <td>搜索条件:</td>
                    <td><input id="searchStr"></td>
                    <td>排序类型:</td>
                    <td>
                        <select id="paixuType">
                            <option value="1" selected="selected">任务名称</option>
                            <option value="2">分组</option>
                            <option value="3">执行IP</option>
                            <option value="4">全局序号</option>
                            <option value="5">最后更新时间</option>
                        </select>
                        <select id="ascFlg">
                            <option value="1" selected="selected">正序</option>
                            <option value="2">倒序</option>
                        </select>
                    </td>
                    <td><input id="searchButn" type="button" value="查询" onclick="searchPage()"></td>
                </tr>
            </table>
            <script>
                //更新查询条件
                function getQueryString(name) {
                    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
                    var r = window.location.search.substr(1).match(reg);
                    if (r != null) return unescape(r[2]); return null;
                }
                var searchStr_url = getQueryString("searchStr");
                var paixuType_url = getQueryString("paixuType");
                var ascFlg_url = getQueryString("ascFlg");
                if(searchStr_url!=null)$("#searchStr").val(decodeURIComponent(searchStr_url));
                if(paixuType_url!=null)$("#paixuType").val(decodeURIComponent(paixuType_url));
                if(ascFlg_url!=null)$("#ascFlg").val(decodeURIComponent(ascFlg_url));

                //绑定查询事件
                function searchPage(){
                    var searchStr = $("#searchStr").val();
                    var paixuType = $("#paixuType").val();
                    var ascFlg = $("#ascFlg").val();
                    window.location.href="./?searchStr="+encodeURIComponent(searchStr)+"&paixuType="+encodeURIComponent(paixuType)+"&ascFlg="+encodeURIComponent(ascFlg);
                }
            </script>
            <%
                String paixuType = request.getParameter("paixuType");
                String searchStr = request.getParameter("searchStr");
                String ascFlg = request.getParameter("ascFlg");
            %>
            <%=tBean.getGlobalTaskInfo(paixuType,searchStr,ascFlg)%>
            </body>
            </html>
        <%
            return;
    }

    if(isLogin&&StringUtils.isNotEmpty(s)&&s.trim().equals("taskTriger")) {
        String globalTaskName = request.getParameter("globalTaskName");
        globalTaskName = URLDecoder.decode(globalTaskName,"utf-8");
        String msg = request.getParameter("msg");
        msg = URLDecoder.decode(msg,"utf-8");
        String globalIndex = request.getParameter("globalIndex");

        String mqProducerGroup = BasisConstants.consumerGroup;
        String instanceName = "finance-payment-instance_001";
        String nameServerAddr = BasisConstants.mqNotifyAddr;
        String mqNotifyGroup = BasisConstants.mqNotifyGroup;
        String businessObjName = globalTaskName.substring(0,globalTaskName.indexOf("_"));
        String globalTaskIndex = globalIndex;//调用第N个全局任务
        boolean trigerIsSuccess = TaskTrigerUtils.trigerTask(mqProducerGroup,instanceName,nameServerAddr,mqNotifyGroup,businessObjName,globalTaskName,"TASKTriger",msg);
        System.out.println("nameServerAddr["+nameServerAddr+"]");
        System.out.println("mqNotifyGroup["+mqNotifyGroup+"]");
        System.out.println("businessObjName["+businessObjName+"]");
        System.out.println("globalTaskIndex["+globalIndex+"]");
        System.out.println("globalTaskName["+request.getParameter("globalTaskName")+"]");
        System.out.println("topic["+"TASK_TRIGER_TOPIC_"+mqNotifyGroup+"]");
        System.out.println("tags["+request.getParameter("globalTaskName").replaceAll("\\.", "_")+"]");
        JSONObject jsonObject = new JSONObject();
        if(trigerIsSuccess){
            jsonObject.put("resCode","0000");
            jsonObject.put("resDesc", "成功");
        }else{
            jsonObject.put("resCode","0001");
            jsonObject.put("resDesc", "失败");
        }
        %>
        <%=jsonObject.toString()%>
        <%
    }
%>
